import gulp from 'gulp'
import uglify from 'gulp-uglify'
import webpack from 'gulp-webpack'
import runSequence from 'run-sequence'
import webpackConfig from './webpack.config.js'
import del from 'del'
import autoprefixer  from 'gulp-autoprefixer'

var elixir = require('laravel-elixir');

const AUTOPREFIXER_BROWSERS = [
  'Android 2.3',
  'Android >= 4',
  'Chrome >= 20',
  'Firefox >= 14',
  'Explorer >= 8',
  'iOS >= 6',
  'Opera >= 12',
  'Safari >= 6'
];

elixir(function(mix) {
    mix.sass('app.scss');
});

gulp.task('clean', () => del.sync(['build/js/*', 'build/css/*', 'build/image/*'], {
  dot: true
}))

gulp.task('bundle', () => {
  let config = webpackConfig
  return gulp
    .src('./app')
    .pipe(webpack(config))
    .pipe(gulp.dest('./build')) 
})

gulp.task('prefix-css', ['bundle'], () => {
  return gulp
    .src('./build')
    .pipe(autoprefixer({
      browsers: AUTOPREFIXER_BROWSERS
    }))
    .pipe(gulp.dest('build/'))
})

gulp.task('uglify', ['bundle'], () => {
  return gulp
    .src('./build/js/*.js')
    .pipe(uglify())
    .pipe(gulp.dest('./build/js'))
})

gulp.task('build', ['clean', 'bundle', 'prefix-css', 'uglify'])



gulp.task('dev', ['clean', 'bundle', 'prefix-css'])

gulp.task('watch', ['clean'], () => {
  let config = webpackConfig
  config.watch = true
  config.progress = true
  return gulp
    .src('./app')
    .pipe(webpack(config))
    .pipe(gulp.dest('./build'))
})

